# ————————————————————  8.2.1文件的打开与关闭  ————————————————————
# file1 = open('a.txt')				# 以只读方式打开文本文件a.txt
# file2 = open('b.txt', 'w')			# 以只写方式打开文本文件b.txt
# file3 = open('c.txt', 'w+')			# 以读/写方式打开文本文件c.txt
# file1 = open('a.txt', 'wb+')		# 以读/写方式打开二进制文件a.txt
# file1.close()

# with open('a.txt') as file:
#     pass


# ————————————————————  8.2.2读文件  ————————————————————
# file = open('demo.txt', encoding='utf-8')
# result = file.read(6)               # 读取6个字符
# print(result)
# result = file.read(6)               # 继续读取6个字符
# print(result)
# result = file.read()                # 读取剩余全部字符
# print(result)
# result = file.read()                # 再次读取，读取不到任何内容
# print(result)
# file.close()


# file = open('demo.txt', encoding='utf-8')
# result = file.readline()                # 第1次读取，读取第一行内容
# print(result)
# result = file.readline()                # 第2次读取，读取第二行内容
# print(result)
# result = file.readline()                # 第3次读取，读取第三行内容
# print(result)
# result = file.readline()                # 第4次读取，读取不到任何内容
# print(result)
# file.close()

# file = open('demo.txt', encoding='utf-8')
# result = file.readlines()   # 一次性从文件中读取所有数据
# print(result)
# print(type(result))          # 查看读取结果的数据类型
# file.close()


# ————————————————————  8.2.3写文件  ————————————————————
# file = open('demo_new.txt', 'w+', encoding='utf-8')                   # 以读写方式打开文件
# file.write("骐骥一跃，不能十步；驽马十驾，功在不舍。")        # 向文件中写入一部分数据
# file.write("锲而舍之，朽木不折；锲而不舍，金石可镂。")   # 向文件中继续写入一部分数据
# file.write("\n荀子《劝学篇》")  # 向文件中的下一行写入一部分数据
# while True:
#     print('我是死循环')

# 以读写模式打开文件，设置缓冲方式
# file = open('demo_new.txt', 'w+', 1, encoding='utf-8')
# file.write("骐骥一跃，不能十步；驽马十驾，功在不舍。")
# file.write("锲而舍之，朽木不折；锲而不舍，金石可镂。")
# file.write("\n荀子《劝学篇》")
# while True:
#     print('我是死循环')

# file = open('demo_new.txt', 'w+', encoding='utf-8')
# file.write("骐骥一跃，不能十步；驽马十驾，功在不舍。")
# file.write("锲而舍之，朽木不折；锲而不舍，金石可镂。")
# file.write("\n荀子《劝学篇》")
# file.flush()   # 刷新缓冲区
# while True:
#     print('我是死循环')

# with open('demo_new.txt', 'w+', encoding='utf-8') as file:
#     file.write("骐骥一跃，不能十步；驽马十驾，功在不舍。")
#     file.write("锲而舍之，朽木不折；锲而不舍，金石可镂。")
#     file.write("\n荀子《劝学篇》")


# ————————————————————  8.2.4文件读写位置  ————————————————————
# file = open('demo.txt', encoding='utf-8')
# location = file.tell()    # 获取文件当前的读写位置
# print(location)
# file.read(5)
# location = file.tell()    # 获取文件当前的读写位置
# print(location)
# file.close()

# file = open('demo.txt', encoding='utf-8')
# file.seek(6, 0)          # 从开头处偏移7个字符
# result = file.read(5)   # 从当前读写位置读取数据
# print(result)
# file.close()

# file = open('demo.txt', encoding='utf-8')
# file.seek(6, 1)          # 从开头处偏移7个字符
# result = file.read(5)   # 从当前读写位置读取数据
# print(result)
# file.close()

# file = open('demo.txt', 'rb')    # 以二进制、只读方式打开文本
# location = file.seek(5, 0)
# print(location)
# location = file.seek(4, 1)
# print(location)
# location = file.seek(5, 2)
# print(location)
# location = file.seek(-3, 2)
# print(location)
# file.close()


# ————————————————————  8.2.5文件与目录管理  ————————————————————
import os
# os.remove('demo_new.txt')
# os.rename('a.txt', 'demo_new.txt')

# os.mkdir('a')
# os.rmdir('a')

# result = os.getcwd()
# print(result)

# os.chdir('E:\\')                      # 更改默认目录
# result = os.getcwd()                 # 获取当前目录
# print(result)

dirs = os.listdir('./')
print(dirs)

